package easier.fast.page.pull;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.RecyclerView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import easier.fast.R;
import easier.fast.base.BaseActivity;
import easier.recycler.MultiTypeAdapter.BaseViewHolder;
import easier.recycler.MultiTypeAdapter.TypeViewHolder;
import io.reactivex.rxjava3.core.Observable;
import java.util.List;

/**
 * 可以侧滑关闭,自带标题栏,需要自己实现,内容布局,设置标题
 */
public abstract class BaseRefreshTitleActivity< T > extends BaseActivity {

    protected SmartRefreshLayout mRefresh;
    protected RecyclerView mRecycler;
    protected Manager mManager;

    @Override
    protected void onCreate( @Nullable Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
    }

    @Override
    protected int getLayoutId() {

        return R.layout.activity_base_title_refresh;
    }

    @Override
    protected void initView() {

        super.initView();

        mRefresh = (SmartRefreshLayout) findViewById( R.id.refresh );
        mRecycler = (RecyclerView) findViewById( R.id.recycler );
    }

    @Override
    protected void initViewAction() {

        mManager = new Manager( mRefresh, mRecycler, getLifecycle() );
        mManager.start( 300 );
    }

    protected void start() {

        if ( mRefresh != null ) {
            mRefresh.autoRefresh();
        }
    }

    /**
     * @return 创建数据请求
     */
    protected abstract Observable< T > createRequest( Context context );

    /**
     * @param context context
     *
     * @return 创建内容布局
     */
    protected abstract List< TypeViewHolder > createContentView( Context context, T t );

    /**
     * @param context 创建没有数据界面
     *
     * @return 没有数据界面
     */
    protected TypeViewHolder createNoDataView( Context context ) {

        return new BaseViewHolder( R.layout.common_no_data );
    }

    protected TypeViewHolder createNetErrorView( Context context ) {

        return new BaseViewHolder( R.layout.common_net_error );
    }

    /**
     * 管理请求
     */
    private class Manager extends RefreshRecyclerManager< T > {

        public Manager( SmartRefreshLayout refresh, RecyclerView recycler, Lifecycle lifecycle ) {

            super( refresh, recycler, lifecycle );
        }

        @Override
        protected Observable< T > createRequest( Context context ) {

            return BaseRefreshTitleActivity.this.createRequest( context );
        }

        @Override
        protected List< TypeViewHolder > createContentView( Context context, T t ) {

            return BaseRefreshTitleActivity.this.createContentView( context, t );
        }

        @Override
        protected TypeViewHolder createNetErrorView( Context context ) {

            return BaseRefreshTitleActivity.this.createNetErrorView( context );
        }

        @Override
        protected TypeViewHolder createNoDataView( Context context ) {

            return BaseRefreshTitleActivity.this.createNoDataView( context );
        }
    }
}